var express = require("express");
var MongoClient = require("mongodb").MongoClient;

var app = express();
var port = 3000;
var url = "mongodb://localhost:27017/"
var dbName = "mydb";
function connect(callback) {
    MongoClient.connect(url, { useUnifiedTopology: true }, function (err, client) {
        if (err) {
            console.log("数据库连接错误！", err);
        } else {
            var db = client.db(dbName);
            callback && callback(db);
            client.close();
        }

    });

}
// 路由
app.get("/", function (req, res) {
    connect(function (db) {
        db.collection("user").find().toArray(function (err, docs) {
            console.log(docs)
            res.send("Hello world!")
        });
    });

});

app.listen(port, function () {
    console.log("http:localhost:%s", port)
});
